package com.rivulet.doj_backend.service.impl;

import com.rivulet.doj_backend.mapper.EmailMapper;
import com.rivulet.doj_backend.pojo.Email;
import com.rivulet.doj_backend.service.EmailService;
import com.rivulet.doj_backend.tools.EmailTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * {邮件功能的实现类}
 *
 * @author xujiac
 * @create 2021/3/29
 * @since 1.0.0
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    EmailMapper emailMapper;

    @Override
    public String sendEmail(Email email) {
        Integer code=new Random().nextInt(899999)+100000;

        emailMapper.setEmailCheckCode(email.getRecipient(),code.toString());
        email.setTitle("账号注册提醒");
        email.setContent(new EmailTemplate(code).toString());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(email.getRecipient());
        message.setSubject(email.getTitle());
        message.setText(email.getContent());
        mailSender.send(message);
        return "success";
    }
}
